// $ = $uery.noConflict();
if (typeof WOW !== "undefined") {
    new WOW().init();
}
/*Carousel Init*/
function init_home_carousel () {
    $("#home_banner").owlCarousel({
        loop: true,
        autoPlay: true,
        items: 1,
        nav: true,
    });
    $("#home_comment").owlCarousel({
        loop: true,
        autoPlay: true,
        items: 1,
        nav: false,
    });
    home_team();
}
function home_team() {
    $("#home_team").owlCarousel({
        margin: 20,
        loop: true,
        autoPlay: true,
        responsive: {
            1140: {items: 4,},
            960:  {items: 3,},
            768:  {items: 2,},
            0:    {items: 1,}
        },
        nav: false,
    });
}
/**
 * 加载动画
 * @returns {boolean}
 */
function start_loop(container = 'body') {
    let dom = "<div class='loading-wrapper'><i class='fa fa-circle-o-notch fa-spin'></i></div>";
    $(container).addClass('position-fixed').append(dom);
    return true;
}

/**
 * 停止加载动画
 * @returns {boolean}
 */
function stop_loop(container = "body") {
    $(container +" .loading-wrapper").fadeOut(function () {
        $(container +" .loading-wrapper").remove();
        $(container).removeClass("position-fixed");
    });
    return true;
}

function submit_success(){
    $('.wpforms-field-container').append(`<div class="alert w-100 alert-success wpforms-success alert-dismissible fade show" role="alert"><i class="fa fa-check mr-1"></i>谢谢您联络我们！ 我们会尽快与您联系。
<button type="button" class="close ml-auto" data-dismiss="alert" aria-label="Close">
    <span aria-hidden="true">&times;</span>
  </button></div>`);
    setTimeout(function(){
        $('.wpforms-success').alert("close");
    },15000)
}

function get_product(cat = false) {
    let tab_content = "#tabs-content .portfolio-main";
    start_loop(tab_content);
    $.post('/wp-admin/admin-ajax.php',{action:"get_product",'cat' : cat},function (e) {
        stop_loop(tab_content);
        $(tab_content).html(e.data);
        $(tab_content +' [data-imagelightbox="f"]').imageLightbox({
            activity: true,
            button: false,
            arrows: false,
            caption: true,
            overlay: true,
            fullscreen: true,
        });
    });
}
$(function () {
    let menuItem = $(".menu-item-has-children");
    //menuItem.find('a').attr('href','javascript:;');
    // 设置导航hover事件
    menuItem.hover(function () {
        $(this).find(".sub-menu").fadeIn('fast');
    },function () {
        $(this).find(".sub-menu").fadeOut('fast');
    });

    $("li.search-item, .search-popup-box").click(function () {
        $("header .popup-content").addClass('active');
        $("body").css({"overflow":"hidden"});
    });
    $(".popup-close").click(function (e) {
        e.stopPropagation();
        $("body").css({"overflow":"auto"});
        $(this).parent().removeClass('active');
    });
    $(".tabnav li > a").click(function () {
        if (!$(this).parent().hasClass("active")) {
            $(this).parent().addClass("active");
            $(this).parent().siblings().removeClass("active");
            //GET DATA
            let cat = $(this).data('cat');
            get_product(cat);
        }

        return true;
    });

    //BootStrap v4 accordion Event

    $("#askAccordion .card-header").click(function () {
       $(this).next().collapse("show");
    });
    $('#askAccordion').on('show.bs.collapse', function (e) {
        $(e.target).prev().addClass("hide-label");
    });
    $('#askAccordion').on('hidden.bs.collapse', function (e) {
        $(e.target).prev().removeClass("hide-label");
    });
    $("#collapseOne").collapse("show");
    /*Contact Form in Home Page*/
    $("button[name='wpforms[submit]']").unbind();
    $("button[name='wpforms[submit]']").click(function (e) {
        let that = $(this);
        e.preventDefault();
        e.stopPropagation();
        $(this).text("发送中...");
        $(".wpforms-submit-spinner").fadeIn('fast');
        let action = $(this).parents("form").data("action")
            ,f = $(this).parents("form").serialize();

        $.post(action,f,function (e) {
            console.log("Success send wpforms!!!");
            $(".wpforms-ajax-form :input").not(":button",":reset","hidden","submit").val("");
            $(".wpforms-submit-spinner").fadeOut('fast');
            submit_success();
            that.text("发送");
        });
        return false;
    });
    //移动端样式&事件
    function mobile_style() {
        const winWidth = $(window).width();
        if (winWidth <= 768) {
            $("header.sticky-top").addClass('shadow');
            //toggle 按钮
            $(".navbar-toggler").click(function () {

                let expand = $(this).attr("aria-expanded");
               if (expand === "false") {

                   $(this).find('span').removeClass('fa-reorder').addClass('fa-close active');
               }else {

                   $(this).find('span').removeClass('fa-close active').addClass('fa-reorder');
               }
            });
            $(".menu-item-has-children").unbind();
            $(".menu-item-has-children").click(function (e) {
                if (e.target.tagName === "I") {
                    e.stopPropagation();
                    e.preventDefault();
                    $(this).find('i').toggleClass('active');
                    if ($(this).find('ul.sub-menu').css("display") === "none") {
                        $(this).find('ul.sub-menu').slideDown('fast');

                    }else {
                        $(this).find('ul.sub-menu').slideUp('fast');
                    }
                }

            });

        }
    }
    mobile_style();
    $(document).scroll(function () {
        // 主菜单效果
        let pattern = $(".navbar-brand").find('img').attr('src');
        let scrollHeight = $(window).scrollTop() ,header = $("header.sticky-top"),
            brand = $(".navbar-brand"),winWidth = $(window).width();
        if (winWidth > 768) {  //PC导航栏样式切换
            if (scrollHeight > 80) {
                //更换背景颜色
                header.addClass("atom-bg-dark");
                //更换图片
                let src = pattern.replace('logo.png','logo-w.png');
                brand.find('img').attr('src',src);
            }else {
                header.removeClass("atom-bg-dark");
                let src = pattern.replace('logo-w.png','logo.png');
                brand.find('img').attr('src',src);
            }
        }

        let progressBar = $('.progress-bar');
        if (progressBar.width() < 20 && $(progressBar.get(0)).offset().top -  $(window).scrollTop() < 500) {
            progressBar.each(function (i) {
                let length = $(this).attr('aria-valuenow');
                let that = $(this);
                setTimeout(function () {
                    that.animate({
                        "width" : length + '%'
                    },{
                        speed: 2,
                        easing: 'linear'
                    },);
                }, i*150)
            })
        }
        //返回顶部效果
        if($(document).scrollTop() >=500) {
            $("#to-top").show();
        }else {
            $("#to-top").hide();
        }
    });
    //返回顶部
    $("#to-top").click(function() {
        $('html,body').animate({scrollTop: 0})
    });

    //tooltips
    $('[data-toggle="tooltip"]').tooltip();
});